/**
 * Copyright [2011] Steffen Kämpke
 * mailto: sk@pennychecker.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pennychecker.example.mvp.view;

import com.pennychecker.example.mvp.model.User;
import com.pennychecker.example.mvp.presenter.AllUserPresenter;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JOptionPane;

/**
 *
 * @author Steffen Kämpke
 */
public class AllUserView extends javax.swing.JPanel implements AllUserPresenter.Display {

    private static final long serialVersionUID = 1L;

    /** Creates new form AllUserView */
    public AllUserView() {
        initComponents();
        this.setName("allUserView");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabelFirstname = new javax.swing.JLabel();
        jLabelLastname = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListUsers = new javax.swing.JList();
        jButtonEdit = new javax.swing.JButton();
        jButtonRemove = new javax.swing.JButton();
        jButtonAdd = new javax.swing.JButton();

        jLabel1.setText("Firstname:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Lastname:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabelFirstname.setName("jLabelFirstname"); // NOI18N

        jLabelLastname.setName("jLabelLastname"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jListUsers.setName("jListUsers"); // NOI18N
        jScrollPane1.setViewportView(jListUsers);

        jButtonEdit.setText("Edit");
        jButtonEdit.setName("jButtonEdit"); // NOI18N

        jButtonRemove.setText("Remove");
        jButtonRemove.setName("jButtonRemove"); // NOI18N

        jButtonAdd.setText("Create");
        jButtonAdd.setName("jButtonAdd"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelFirstname, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelLastname, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonRemove, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonEdit, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabelFirstname))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabelLastname))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonRemove)
                    .addComponent(jButtonEdit)
                    .addComponent(jButtonAdd))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonEdit;
    private javax.swing.JButton jButtonRemove;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabelFirstname;
    private javax.swing.JLabel jLabelLastname;
    private javax.swing.JList jListUsers;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    public JList getUserList() {
        return jListUsers;
    }

    public JButton getAddUserButton() {
        return jButtonAdd;
    }

    public int getSelectedUserIndex() {
        return jListUsers.getSelectedIndex();
    }

    public void setSelectedUser(User selectedUser) {

        if (null == selectedUser) {
            jLabelFirstname.setText("");
            jLabelLastname.setText("");
            return;
        }
        jLabelFirstname.setText(selectedUser.getFirstname());
        jLabelLastname.setText(selectedUser.getLastname());
    }

    public void setUsers(List<User> users) {
        final DefaultListModel model = new DefaultListModel();

        for (int i = 0; i < users.size(); i++) {
            final User user = users.get(i);
            model.add(i, user.getFirstname() + " " + user.getLastname());
        }

        jListUsers.setModel(model);
    }

    public void clearSelections() {
        jListUsers.clearSelection();
    }

    public void setSelection(int index) {
        jListUsers.setSelectedIndex(index);
    }

    public JButton getEditUserButton() {
        return jButtonEdit;
    }

    public JButton getRemoveUSerButton() {
        return jButtonRemove;
    }

    public void setError(String string) {
        JOptionPane.showMessageDialog(this, string, "Error", JOptionPane.ERROR_MESSAGE);
    }

    public void setWarning(String string) {
        JOptionPane.showMessageDialog(this, string, "Warning", JOptionPane.WARNING_MESSAGE);
    }

    public JComponent asComponent() {
        return this;
    }
}
